<?php
namespace app\common\lib;
use think\Log;
class Api{
    //  调用第三方 接口路由
    /**
     * 登陆接口
     */
    const  LOGIN_URL = 'member/investor-login';
    /**
     * 注册开户接口
     */
    const  MERCHANT_REGISTER='member/add-merchant';
    /**
     * 个人商户 实名认证
     */
    const  PERSONAL_REAL = 'member/real-name-verify';
    /**
     * 绑定银行卡
     */
    const  BIND_CARD = 'member/bind-card';
    /**
     * 二次绑卡 推进
     */
    const  BIND_CARD_ADVANCE = 'member/bind-card-advance';
    /**
     * 设置支付密码
     */
    const SET_PAY_PASSWORD = 'member/set-pay-password';
    /**
     * 企业资质申请
     */
    const  VALIDATE_COMPANY_APTITUDE='company/applyCompanyInfo';
    /*
     *借款人注册*/
    const ADD_BORROWER='member/add-borrower';
    /**
     * 充值接口
     */
    const  RECHARGE_MONEY = 'account/recharge';
    
    /**
     * 提现接口
     */
    const  WITHDRAW_MONEY = 'account/withdraw';

    const GET_REPAY_MONEY_BY_DAY = 'merchant/getRepayMoneyByDate';


    const GET = 1;

    const POST = 2;

    const SERVICE = 1;

    const UC_URL = 2;


    /**
     * 执行外网访问
     * @param $api
     * @param $method
     * @param $param
     * @param int $url_select
     * @return bool|mixed
     */
    public static function doRequest($api,$method,$param,$url_select = 1){
        if($method == static::GET){
            return static::doGet($api,$param,$url_select);
        }

        if($method == static::POST){
            return static::doPost($api,$param,$url_select);
        }
        return false;
    }

    private static function getUrlPath($url){
        switch ($url){
            default:
                return config('service_url');
                break;
            case static::UC_URL:
                return config('uc_url');
                break;
        }
    }

    private static function doGet($api,$param,$url){
        $url = static::getUrlPath($url);
        $url.= $api;
        $url = $url.'?'.http_build_query($param);
        Log::write('get_param:'.json_encode($param));
        Log::write('get:'.$url);
        $component = new Component();
        $result = $component->curlget($url);
        Log::write($result);
        return json_decode($result,true);
    }

    private static function doPost($api,$param,$url){
        $url = static::getUrlPath($url);
        $url.= $api;
        $component = new Component();
        $result = $component->post_to_url($url,$param);
        Log::write($result);
        return json_decode($result,true);
    }
}